package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.edu.modules.oa.config.HttpURLConnectionUtil;
import org.edu.modules.oa.entity.VeBaseCampusEntity;
import org.edu.modules.oa.entity.VeBaseDepartmentEntity;
import org.edu.modules.oa.entity.VeBaseTeacherEntity;
import org.edu.modules.oa.entity.bo.BasicResponseBO;
import org.edu.modules.oa.mapper.IVeBaseCampusMapper;
import org.edu.modules.oa.mapper.IVeBaseDepartmentMapper;
import org.edu.modules.oa.mapper.IVeBaseTeacherMapper;
import org.edu.modules.oa.service.IVeBaseCampusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class VeBaseCampusServiceImpl implements IVeBaseCampusService {
    @Value("${common.host}")
    private String dirHost ;

    @Autowired
    ObjectMapper mapper;

    @Resource
    private IVeBaseCampusMapper iVeBaseCampusMapper;
    @Resource
    private IVeBaseDepartmentMapper iVeBaseDepartmentMapper;
    @Resource
    private IVeBaseTeacherMapper iVeBaseTeacherMapper;
    @Override
    public HashMap<String, Object> getCampusUserList() {
        //创建查询的条件，wrapper是条件综合器
        QueryWrapper<VeBaseCampusEntity>CampusWrapper = new QueryWrapper<>();
        List<VeBaseCampusEntity> campusList = iVeBaseCampusMapper.selectList(CampusWrapper);

        QueryWrapper<VeBaseDepartmentEntity>DepartmentWrapper = new QueryWrapper<>();
        List<VeBaseDepartmentEntity>departmentList = iVeBaseDepartmentMapper.selectList(DepartmentWrapper);

        QueryWrapper<VeBaseTeacherEntity>TeacherWrapper = new QueryWrapper<>();
        TeacherWrapper.eq("DQZTM",11);
        List<VeBaseTeacherEntity>TeacherList = iVeBaseTeacherMapper.selectList(TeacherWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("campusList",campusList);
        map.put("departmentList",departmentList);
        map.put("TeacherList",TeacherList);
        return map;
    }

//    @Override
//    public HashMap<String, Object> getCampusUserList() {
//        HashMap<String,Object> map = new HashMap<>();
//        String url = "/common/veCommon/queryCampusList?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
//        BasicResponseBO<List> campus = getUrl(url,List.class);
//        map.put("campusList",campus.getResult());
//        String departUrl = "/common/veCommon/queryDepartmentList?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
//        BasicResponseBO<List> depart = getUrl(departUrl,List.class);
//        map.put("departmentList",depart.getResult());
//        String teacherUrl = "/common/veCommon/getTeacherList?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
//        BasicResponseBO<List> teacher = getUrl(teacherUrl,List.class);
//        map.put("TeacherList",teacher.getResult());
//        return  map;
//    }

    //获取部门和校区集合
    @Override
    public HashMap<String, Object> getCampusDepList() {
        //创建查询的条件，wrapper是条件综合器
        QueryWrapper<VeBaseCampusEntity>CampusWrapper = new QueryWrapper<>();
        List<VeBaseCampusEntity> campusList = iVeBaseCampusMapper.selectList(CampusWrapper);

        QueryWrapper<VeBaseDepartmentEntity>DepartmentWrapper = new QueryWrapper<>();
        List<VeBaseDepartmentEntity>departmentList = iVeBaseDepartmentMapper.selectList(DepartmentWrapper);

        HashMap<String,Object> map = new HashMap<>();
        map.put("campusList",campusList);
        map.put("departmentList",departmentList);
        return map;
    }

//    @Override
//    public HashMap<String, Object> getCampusDepList() {
//        HashMap<String,Object> map = new HashMap<>();
//        String url = "/common/veCommon/queryCampusList?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
//        BasicResponseBO<List> campus = getUrl(url,List.class);
//        map.put("campusList",campus.getResult());
//        String departUrl = "/common/veCommon/queryDepartmentList?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
//        BasicResponseBO<List> depart = getUrl(departUrl,List.class);
//        map.put("departmentList",depart.getResult());
//        return map;
////        return b;
//    }
    /**
     * 调用公告数据接口
     */
    private <T> BasicResponseBO<T> getUrl(String url, Class<T> clazz) {
        try {
            url = dirHost+url ;
            String result = HttpURLConnectionUtil.doGet(url) ;
            BasicResponseBO<T> re = mapper.readValue(result, new TypeReference<BasicResponseBO<T>>() {});
            T body = mapper.readValue(mapper.writeValueAsString(re.getResult()), clazz);
            re.setResult(body);
            return re;
        } catch (Exception e) {
            return new BasicResponseBO<T>().setSuccess(false).setMessage("操作失败");
        }
    }
}
